package com.wectrls.service.impl;

import com.wectrls.common.functionalInterface.FieldNameUtils;
import com.wectrls.common.utils.DateUtils;
import com.wectrls.config.exceptiom.CommonException;
import com.wectrls.entity.Category;
import com.wectrls.entity.Sources;
import com.wectrls.enums.COMMON_STATUS_ENUM;
import com.wectrls.enums.RESPONSE_ENUM;
import com.wectrls.mapper.SourcesMapper;
import com.wectrls.service.*;
import com.wectrls.vo.postvo.SourcesAddVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Optional;

/**
 * @author chenjian
 * @Package
 * @Description:
 * @date 2019-10-31 0:11
 */
@Service
@Transactional
public class SourcesServiceImpl implements SourcesService {

    @Autowired
    private WecsUserService wecsUserServiceImpl;

    @Autowired
    private SourcesMapper sourcesMapper;

    @Autowired
    private CategoryService categoryServiceImpl;

    @Autowired
    private UserGroupService userGroupServiceImpl;

    @Autowired
    private PageService pageServiceImpl;

    /**
     * 新增资源
     *
     * @param sourcesAddVo
     * @return
     */
    @Override
    public Sources addOne(SourcesAddVo sourcesAddVo) {

        //新增资源
        @NotNull String categoryId = sourcesAddVo.getCategoryId();
        Category category = categoryServiceImpl.findById(categoryId);
        Sources sources = new Sources();
        sources.setCategoryId(category.getId());
        sources.setSourcesName(sourcesAddVo.getSourcesName());
        sources.setSourcesUrl(sourcesAddVo.getSourcesUrl());
        sources.setCTime(DateUtils.getTimeMillis());
        sources.setSortNum(sourcesAddVo.getSortNum());
        sources.setSourcesDesc(sourcesAddVo.getSourcesDesc());
        sources.setStatus(COMMON_STATUS_ENUM.NORMAL.getStatus());
        Sources saveAndFlush = sourcesMapper.saveAndFlush(sources);
        return saveAndFlush;
    }

    @Override
    public List<Sources> findByCategoryId(String categoryId, Sort sort) {
        List<Sources> sourcesList = sourcesMapper.findByCategoryId(categoryId, sort);
        return sourcesList;
    }

    @Override
    public Sources findById(String sourcesId) {
        Optional<Sources> optional = sourcesMapper.findById(sourcesId);
        if (optional.isPresent()) {
            return optional.get();
        }
        throw new CommonException("无相关资源", RESPONSE_ENUM.FAIL_500.getCode());
    }

    @Override
    public Sources saveOrUpdate(Sources sources) {
        Sources saveAndFlush = sourcesMapper.saveAndFlush(sources);
        return saveAndFlush;
    }

    @Override
    public void delSourcesByCategoryId(String id) {
        List<Sources> sourcesList = sourcesMapper.findByCategoryId(id, Sort.by(FieldNameUtils.convertToFieldName(Sources::getSortNum)).ascending());
        for (Sources sources : sourcesList) {
            sourcesMapper.delete(sources);
        }
    }
}
